﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TwikiLinkScanners;
using HtmlAgilityPack;
using System.IO;

namespace t_TwikiLinkScanners
{
    [TestClass]
    public class t_TwikiParseHelpers
    {
        [TestMethod]
        public void TestExtractATLASPhysicsPage()
        {
            string fname = "..\\..\\..\\t_TwikiLinkScanners\\ATLASTopPhysics.html";
            string[] expected = new string[] { "TWiki", "Atlas Web", "AtlasResults", "TopPublicResults", "RichardHawkings" };
            TestBreadCrumbs(fname, expected);
        }

        [TestMethod]
        public void TestExtractALTASDetectorPage()
        {
            string fname = "..\\..\\..\\t_TwikiLinkScanners\\ATLASInnerDetector.html";
            string[] expected = new string[] { "TWiki", "Atlas Web", "AtlasResults", "InnerDetPublicResults", "PippaWells"};
            TestBreadCrumbs(fname, expected);
        }

        [TestMethod]
        public void TestGOODATLASCrumbs()
        {
            Assert.IsTrue(TwikiParseHelpers.isPublicSetOfTwikiBreadcrumbTrail(new string[] {"Twiki", "Atlas Web", "AtlasResults", "InnerDetPublicResults", "PippaWells"}), "Inner detector bread crumb should be ok");
            Assert.IsTrue(TwikiParseHelpers.isPublicSetOfTwikiBreadcrumbTrail(new string[] { "Twiki", "Atlas Web", "DataPreparation", "ApprovedDetectorPlots", "ApprovedPlotsPixel" }), "pixel bread crumb should be ok");
            Assert.IsTrue(TwikiParseHelpers.isPublicSetOfTwikiBreadcrumbTrail(new string[] { "Twiki", "Atlas Web", "DataPreparation", "ApprovedDetectorPlots", "ApprovedPlotsSCT" }), "sct bread crumb should be ok");
            Assert.IsTrue(TwikiParseHelpers.isPublicSetOfTwikiBreadcrumbTrail(new string[] { "Twiki", "Atlas Web", "AtlasDetectors", "InnerDetector", "InDetTrackingPerformance", "InDetTrackingPerformanceCSCFigures", "PatrickJussel" }), "InDetTrackingPerformanceCSCFigures bread crumb should be ok");
            Assert.IsTrue(TwikiParseHelpers.isPublicSetOfTwikiBreadcrumbTrail(new string[] { "Twiki", "Atlas Web", "TriggerDAQ", "TapmMainPage", "TriggerCSCNotes", "PatrickJussel" }), "CSC InDet bread crumb should be ok");
            Assert.IsTrue(TwikiParseHelpers.isPublicSetOfTwikiBreadcrumbTrail(new string[] { "Twiki", "Atlas Web", "AtlasReultsEcmDependence", "PatrickJussel" }), "Ecm studies bread crumb should be ok");
        }

        [TestMethod]
        public void TestBADATLASCrumbs()
        {
            Assert.IsFalse(TwikiParseHelpers.isPublicSetOfTwikiBreadcrumbTrail(new string[] { "Twiki", "Atlas Web", "AtlasDetectors", "MuonSpectrometer", "HenryBachacou" }), "CSC InDet bread crumb should be ok");
            Assert.IsFalse(TwikiParseHelpers.isPublicSetOfTwikiBreadcrumbTrail(new string[] { "Twiki", "Atlas Web", "AtlasDetectors", "MuonSpectrometer", "MuonSoftware", "MuonSoftwareReleaseSignOff", "HenryBachacou" }), "CSC InDet bread crumb should be ok");
        }

        private static void TestBreadCrumbs(string fname, string[] expected)
        {
            HtmlDocument doc = new HtmlDocument();
            FileInfo f = new FileInfo(fname);
            using (var r = f.OpenText())
            {
                doc.LoadHtml(r.ReadToEnd());
                r.Close();
            }
            var result = TwikiParseHelpers.ExtractATLASTwikiBreadCrumbs(doc.DocumentNode).ToArray();
            Assert.AreEqual(expected.Length, result.Length, "Unexpected # of bread crumbs");
            var joined = expected.Zip(result, (s1, s2) => new string[] { s1, s2 });
            foreach (var item in joined)
            {
                Assert.AreEqual(item[0], item[1], "Unequal bread crumbs!");
            }
        }
    }
}
